package com.engine.jucailinkq.attendance.vacation.cmd;

import com.engine.jucailinkq.attendance.vacation.job.holidaygeneration.tactics.HolidayGenerationWay;
import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.jucailinkq.common.util.DbTools;
import com.engine.jucailinkq.common.util.Utils;
import com.engine.core.interceptor.CommandContext;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import weaver.general.Util;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class VocationCmd extends AbstractCommonCommand<Map<String,Object>> {
    @Override
    public BizLogContext getLogContext() {
        return null;
    }
    public VocationCmd(Map<String,Object> params){
        this.params=params;
    }

    private List<HolidayGenerationWay> rulers;
    {
        rulers = new Utils<HolidayGenerationWay>().getAllClassByInterface(HolidayGenerationWay.class);

    }
    @Override
    public Map<String, Object> execute(CommandContext commandContext) {
        List<Map<String,Object>> vocationList = (List<Map<String,Object>>)params.get("vocationList");
        List<Map<String,Object>> userList = (List<Map<String,Object>>)params.get("userList");
        List<Map<String,Object>> userfilter = userList.stream().filter(e->"-1".equals(e.get("scopeid"))).collect(Collectors.toList());
        Map<String,Object> userMap = userfilter.size()>0?userfilter.get(0):Maps.newHashMap();

        for (Map<String,Object> vocation : vocationList){
            log.debug("qsrq : [{}],yjzd : [{}]",vocation.get("qsrq"),vocation.get("yjzd"));
            String qsrq = Util.null2String(vocation.get("qsrq")).toLowerCase();
            String yjzd = Util.null2String(vocation.get("yjzd")).toLowerCase();
            String ljcrglyfslwz = Util.null2String(vocation.get("ljcrglyfslwz"));
            String qsrqValue = Util.null2String(userMap.get(qsrq));
            String yjzdValue = Util.null2String(userMap.get(yjzd));
            String ljcrglyfslwzValue = Util.null2String(userMap.get(ljcrglyfslwz));
            if ("".equals(qsrqValue)){
                for (Map<String,Object> map: userList){
                    qsrqValue = Util.null2String(map.get(qsrq));
                    if (!"".equals(qsrqValue)){
                        break;
                    }
                }
            }
            if ("".equals(yjzdValue)){
                for (Map<String,Object> map: userList){
                    yjzdValue = Util.null2String(map.get(yjzd));
                    if (!"".equals(yjzdValue)){
                        break;
                    }
                }
            }
            if ("".equals(ljcrglyfslwzValue)){
                for (Map<String,Object> map: userList){
                    ljcrglyfslwzValue = Util.null2String(map.get(ljcrglyfslwz));
                    if (!"".equals(ljcrglyfslwzValue)){
                        break;
                    }
                }
            }


            vocation.put("qsrqValue",qsrqValue);
            vocation.put("yjzdValue",yjzdValue);
            vocation.put("ljcrglyfslwzValue",ljcrglyfslwzValue);
            vocation.put("zyzValue",getUserIds(vocation));
        }
        log.debug("VocationCmd params: [{}]",params);
        String edfffs = Util.null2String(vocationList.get(0).get("edfffs"));
        for (HolidayGenerationWay holidayGenerationWay :rulers){
            if (holidayGenerationWay.support(Integer.valueOf(edfffs))){
                holidayGenerationWay.compute(params);
            }
        }

        return null;
    }

    public List<String> getUserIds(Map<String,Object> vocation){
        List<String> userIds = Lists.newArrayList();
        String edyj = Util.null2String(vocation.get("edyj"));
        if (edyj.equals("3") || edyj.equals("4")){
            String zyz = Util.null2String(vocation.get("zyz"));
            zyz = zyz.replace("ａｎｄ","and");
            zyz = zyz.replace("ｏｒ","or");
            List<Map<String,Object>> dataList =  DbTools.getSqlToList(zyz);
            userIds = dataList.stream().map(e->Util.null2String(e.get("id"))).collect(Collectors.toList());
        }
        return userIds;
    }
}
